//
//  MessageUtils.m
//
//  Created by Edenyorke on 14/12/11.
//  Copyright (c) 2011 Edenyorke. All rights reserved.
//

#import "MessageUtils.h"

@implementation MessageUtils

- (void)initData {
    
    NSString *path = [self getDocumentPath];
    NSMutableDictionary *unDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    if (!unDict){
        unDict = [[NSMutableDictionary alloc]init];
        [unDict setValue:@"" forKey:@"dateDebut"]; 
        [unDict setValue:@"" forKey:@"dateFin"];
        [unDict setValue:@"" forKey:@"periodeType"]; 
        [unDict setValue:@"" forKey:@"periodeNumber"]; 
        [unDict setValue:@"YES" forKey:@"isActive"]; 
        [unDict setValue:@"2.0" forKey:@"version"];
        [unDict writeToFile:path atomically:YES];
        
    }

    
}

-(NSString*) getDocumentPath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"tourdegarde.plist"];
    
    
    return path;
    
}


- (NSString*) getLabel:(NSString *)key{
    
    NSString *path = [self getDocumentPath];
    NSMutableDictionary *dico = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    if (!dico){
        [self initData];
    }
    NSString *value = [dico valueForKey:key];
    return value;

    
}


- (BOOL) getBool:(NSString *)key{
    
    NSString *path = [self getDocumentPath];
    NSMutableDictionary *dico = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    if (!dico){
        [self initData];
    }
    NSString *value = [dico valueForKey:key];
    BOOL boolValue = [value boolValue];
    return boolValue;
    
    
}

- setValueForLabel:(NSString *)key value:(NSString*)value{
    
    NSString *path = [self getDocumentPath];
    NSMutableDictionary *dico = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    if (!dico){
        [self initData];
    }
    [dico setValue:value forKey:key];
    [dico writeToFile:path atomically:YES];
     
    
    
    return nil;
}

- setArrayForLabel:(NSString *)key value:(NSMutableArray*)values{
    
    NSString *path = [self getDocumentPath];
    NSMutableDictionary *dico = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    if (!dico){
        [self initData];
    }
    NSLog(@"ListCustody : %d",[values count]);
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:values];
    [dico setValue:data forKey:key];
    [dico writeToFile:path atomically:YES];
    
    
    
    return nil;
}


- (NSMutableArray*) getArray:(NSString *)key{
    
    NSString *path = [self getDocumentPath];
    NSMutableDictionary *dico = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    if (!dico){
        [self initData];
    }
    NSData *data = [dico valueForKey:key];
    NSMutableArray *values = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    if(values == nil){
        values = [[NSMutableArray alloc]init];
    }
    return values;
    
    
}

@end
